<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class MessageController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //发送消息
        return view('/admin/message/create');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
//        dd($request ->all());
        //如果是超级会员查询所有数据
        $receive_id =session('member_id');
            $data = DB::table('message')->where('readed',1)->where('receive_mem_id',$receive_id)->where(function($query) use ($request){
                $query -> where('content','like','%'.$request -> input('keywords').'%');
            }) -> paginate($request -> input('num',3));
            foreach($data as $v)
            {

             //取出发信人用户名
                $send_mem_id  = $v -> send_mem_id;
                //获取收信人名
                $receive_id =$v -> receive_mem_id;
                $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
                $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
                $v->sname = $arr->name;
                $v->rname = $arr1->name;
                //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
                $v->stime = date('Y-m-d H:i:s',$v->stime);
                $v->content = mb_substr(trim($v->content),0,20);
            }
        return view('/admin/message/index',['data'=>$data,'request' => $request -> all()]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //处理添加
        $data = $request -> all();
//        dd($data);
        $name=$data['name'];
        $content=$data['content'];
        unset($data['_token']);
        if($name == 'all')
        {
            unset($data['name']);
            $all_id=DB::table('member')->select('member_id')->get();
            foreach($all_id as $v)
            {
                $data['receive_mem_id'] = $v->member_id;
                if($data['receive_mem_id'] == $data['send_mem_id']){
                    continue;
                }
                $res = DB::table('message')->insert($data);
            }
        }else {
            $userarr = DB::table('member')->select('name')->get();
            foreach ($userarr as $v) {
                $names[] = $v->name;
            }
            if (!in_array($name, $names)) {
                return redirect('/admin/message/create')->with(['success' => '收件人不存在',
                    'content' => $content]);
            };
            $arr = DB::table('member')->where('name', $name)->first();
            unset($data['name']);
            $data['receive_mem_id'] = $arr->member_id;
            $data['stime'] = time();
            $res = DB::table('message')->insert($data);
        }
        if($res)
        {
            return redirect('/admin/message/create') -> with(['success'=>'发送成功']);
        }else{
            return redirect('/admin/message/create') -> with(['success'=>'发送失败']);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //消息详情
        $data = DB::table('message')->where('receive_mem_id',$id) ->get();
        //根据ID获取发件人、收件人name
        $arr = DB::table('member') -> where('member_id',$data['0'] ->send_mem_id) -> first();
        $arr1 = DB::table('member') -> where('member_id',$data['0'] ->receive_mem_id) -> first();
        //更改为已读
        DB::table('message')->where('receive_mem_id',$id)->where('send_mem_id',$data['0'] ->send_mem_id) ->update(['readed'=>2]);
        $data['0'] ->sname = $arr->name;
        $data['0'] ->rname = $arr1->name;
        $data['0'] ->time = $data['0']->stime;
        $data['0']->stime = date('Y-m-d H:i:s',$data['0']->stime);
        $data=$data['0'];
        return view('/admin/message/show',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除消息
        $res=DB::table('message')->where('stime',$id)->update(['disabled'=>2]);
        if($res)
        {
            return redirect('/admin/message') -> with(['success'=>'已移入回收站']);
        }else{
            return redirect('/admin/message') -> with(['success'=>'删除失败']);
        }

    }

    //收件箱
    public function receive(Request $request)
    {
        $receive_id =session('member_id');
        $data = DB::table('message')->where('receive_mem_id',$receive_id)->where(function($query) use ($request){
            $query -> where('content','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',3));
        foreach($data as $k=>$v)
        {

            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/admin/message/receive',['data'=>$data,'request' => $request -> all()]);
    }
    //已发送
    public function send(Request $request)
    {
        $send_id =session('member_id');
        $data = DB::table('message')->where('send_mem_id',$send_id)->where(function($query) use ($request){
            $query -> where('content','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',3));
        foreach($data as $k=>$v)
        {
            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/admin/message/send',['data'=>$data,'request' => $request -> all()]);
    }

    //已删除
    public function delete(Request $request)
    {
        $send_id =session('member_id');
        $data = DB::table('message')->where('receive_mem_id',$send_id)
            ->orwhere('send_mem_id',$send_id)
            ->where('disabled',2)
            ->where(function($query) use ($request){
            $query -> where('content','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',3));
        foreach($data as $k=>$v)
        {
            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->time=$v->stime;
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/admin/message/delete',['data'=>$data,'request' => $request -> all()]);
    }

    //执行删除
    public function dodelete($id)
    {
        $send_id =session('member_id');
        $res=DB::table('message')
            ->where('receive_mem_id',$send_id)
            ->orwhere('send_mem_id',$send_id)
            ->where('stime',$id)->delete();
        if($res)
        {
            return redirect('/admin/message') -> with(['success'=>'删除成功']);
        }else{
            return redirect('/admin/message/delete') -> with(['success'=>'删除失败']);
        }
    }
}
